$(document).ready(function(){
	$.ui.showMask("加载中");
	document.title = "心愿墙";
	var parm = getRequestParam();
	if(parm["code"] !=undefined && parm["code"] !="" && wecharNameVal == ""){
		queryWechatInfo(parm["code"]);
	}
	if(location.href.indexOf("type=fenxiang") > -1 && parm["wishOrderId"] !=undefined && parm["wishOrderId"] !=""){
		queryWishOrderById(parm["wishOrderId"]);
	}
	if(location.href.indexOf("type=xuyuam") > -1 && parm["wishOrderId"] !=undefined && parm["wishOrderId"] !=""){
		queryWishOrderByIdForXuYuan(parm["wishOrderId"]);
	}
	queryMenu(programCodeVal);
	$("#menu nav ul.list span").click(function(){
		$(this).addClass("selectd").siblings().removeClass("selectd");
		var ln=$("#menu nav ul.list span").index(this);
		$("#menu nav ul.list li").eq(ln).toggle().siblings("li").hide();
	})
	$("#menu nav ul.list li a").click(function(){
		$(this).addClass("selectd").siblings().removeClass("selectd");
	})
	initFengXiang();
	$.ui.hideMask();
	
	if(location.href.indexOf("type=main") > -1){
		$.ui.loadContent("#main",false,false,"slide");
	}
});
function initData(){
	$.ui.launch();
	$.ui.toggleNavMenu();	
	
	$("#protuct_list section div").click(function(){
	   footer_s();
	   var selLength=$(".footers_tree ul").find("img").length;
	   var divno=$(this).parents(".wif_div").attr("id");
	   if($(this).hasClass("sele")){
			$(this).removeClass("sele");
			$(this).parent().find(".img_mv").removeClass("imgmover");
			$(".li_"+divno).html("");
			$(".li_"+divno).removeAttr("class");
		}else if(selLength<3){
			$(this).addClass("sele");
			var tsrc=$(this).parent().find("img").attr("src");
			for(var i=0;i<3;i++){
				if($(".footers_tree ul li").eq(i).html()==""){								
					$(".footers_tree ul li").eq(i).html("<img src='"+tsrc+"'/>");
					$(".footers_tree ul li").eq(i).addClass("li_"+divno);					
	   				$(this).parent().find(".img_mv").addClass("imgmover");
					break;
				}
			}
	   }else{
			$("#task").show();
			$(this).removeClass("sele");
	   }
	})
	$("#menu nav ul li a").click(function(){
		$.ui.toggleSideMenu();
	})
	$(".footers_tree ul li").click(function(){
		$("#"+$(this).attr("class").substring(3)).find("div.sele").removeClass("sele");
		$("#"+$(this).attr("class").substring(3)).find(".img_mv").removeClass("imgmover");
		$(this).html("");
		$(this).removeAttr("class");
	})
	$(".footers_tree a").click(function(){
		$.ui.clearHistory();						
		$.ui.toggleNavMenu();
		$(".footers_tree").hide();
		var totree_img=$(".footers_tree ul").find("img");
		for(var i=0;i<totree_img.length;i++){
			var tsrc=totree_img.eq(i).attr("src");
			$("ul.tree_pro a").eq(i).html("<img id='productionId_"+totree_img.eq(i).parent().attr("class").split("_")[1]+"' src=\"images\/pao.png\"><img src='"+tsrc+"'/>");
		    $("ul.tree_pro a").eq(i+3).html("<img id='productionId_"+totree_img.eq(i).parent().attr("class").split("_")[1]+"' src=\"images\/pao.png\"><img src='"+tsrc+"'/>");
		}
					
	})
	
	$("a.pro_plist").click(function(){
		$(this).parent().find(".pro_plist_detail").toggle();		
		$("#mywishSwipe >.swipe-wrap .swipe-wrap> div").css("-webkit-transform","inherit");
	});
	$("a.pro_plists").click(function(){
		$(this).parents(".pro_plist_detail").toggle();
	})
	$("a.xuruan").click(function(){
		$("#xuyan").show();
	})
	$(".remark > span").click(function(){
		$.ui.showMask("加载中");
		queryWechatInfo("");
		if(subscribeStr == '1'){
			$("#zhufu").show();
			$.ui.hideMask();
		}else{
			$.ui.hideMask();
			window.location.href = guanZhuUrl;
		}
	})
	$(".showdiv").click(function(){
		$(this).hide();
	})
	window.onload=function()
	{	 	
		 protuct_list();
		 tree_list();
		 if($("#pro_detail").css("display")=="block"){
		  	new_swiper();
		  }
		 window.setInterval(function(){
			  footer_s();
			  if($("#pro_detail").css("display")=="block"){
				  if($("#mywishSwipe>.swipe-wrap").width()==$("#afui").width()){	
					  new_swiper();
				  }
//				  $.ui.hideMask();
			  }
			  if($("#tree").css("display")=="block" || $("#trees").css("display")=="block"){
				  for(var i=0;i<$(".the_tree ul.tree_pro li").length;i++){
					  $("#afui.Joyo .the_tree ul.tree_pro li").eq(i).height($("#afui.Joyo .the_tree ul.tree_pro li").eq(i).width());
						$(".scrolls").height($("#afui.Joyo").height()*.22);
				  }
				  $("#trees .the_tree ul.tree_pro li a").click(function(){		  
					  $.ui.showMask("加载中");
				  });
			  }
			  if($("#main").css("display")=="block" && $("#protuct_list .wif_div").eq(0).height()!=$("#afui").width()){				  
			  	 protuct_list()	
			  }
		 	 buttons();
		  }, 1000);	
	}
	window.onresize = function () {
		tree_list();
	}
 
};
function buttons(){
	$("a.button.add_tree,a.button.now_wish,a.button.xuruan_ban,a.button.xuruan_me").width(($("#afui").width()-30)/2);
	$("a.button.zhichi").click(function(){
		$(this).addClass("zhichi_c").removeClass("zhichi");
		$(this).text("谢谢:)");
	})
}
function tsdiv(a){
	$.ui.showMask("加载中");
	//queryWechatInfo("");
	$.ajax({ 
        type: "POST",
        async: true, 
        url: baseUrl + "/json/wishwall/wishOrder_checkIsGuanzhu", 
        dataType: "json", 
        data : "code=&openId="+wechatIdStr,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert("error!"+errorThrown); 
        },
        success: function (data) {
        	if(data.state == '1'){
        		wechatIdStr = data.openId;
        		subscribeStr = data.subscribe;
        		wecharNameVal = data.nickName;
        		wecharNameValForOther = data.nickName;
        	}else if(data.state == '0'){
        		$("#id_alertMsg").html("faile!");
        		$("#task_alertMsg").show();
        	}
        	if(subscribeStr == '1'){
        		$.ui.loadContent("#main",false,false,"slide");
        	}else{
        		window.location.href = guanZhuUrl;
        	}
        	$.ui.hideMask();
        }
	});
}
function footer_s(){
	if($("#main").css("display")=="block" && $("#menu").css("display")!="block"){
	  for(var i=0;i<$(".footers_tree ul li").length;i++){
		  if($(".footers_tree ul li").eq(i).html()==""){								
			  $(".footers_tree").hide();
		  }else{
			  $(".footers_tree").show();
			  break;
		  }
	  }
	}else{
	  $(".footers_tree").hide();
	}
	if($("#tree").css("display")=="block" || $("#trees").css("display")=="block"){
		$("#afui").addClass("tree_show");
	}else{
		$("#afui").removeClass("tree_show");
	}
}
function tree_list(){				
	for(var i=0;i<$("#afui.Joyo .the_tree ul.tree_pro li").length;i++){		
		$("#afui.Joyo .the_tree ul.tree_pro li").eq(i).height($("#afui.Joyo .the_tree ul.tree_pro li").eq(i).width())
	}
	$(".remark").width($("#afui").width()-20);
}
function protuct_list(){	
	var margins=10;
	var li_w=($("#afui.Joyo").width()-30)/2;
	//var li=document.getElementById("protuct_list").getElementsByClassName("wif_div");
	var li=$("#protuct_list .wif_div");		   		   
	for(var i=0;i<li.length;i++){
	   li.eq(i).width(li_w);
		var li_height=0;
		if(i<2){			
			//li[i].style.top="10px";
			//li[i].style.left=i*li_w+margins*(i+1)+"px";	
			li.eq(i).css("top","10px");
			li.eq(i).css("left",i*li_w+margins*(i+1));						
		}else if(i%2){				
			//li[i].style.top=li[i-2].offsetHeight+parseInt(li[i-2].style.top)+margins+"px";
			//li[i].style.left=li_w+margins*2+"px";
			li.eq(i).css("top",li.eq(i-2).height()+parseInt(li[i-2].style.top)+margins);
			li.eq(i).css("left",li_w+margins*2);	
		}else{
			//li[i].style.top=li[i-2].offsetHeight+parseInt(li[i-2].style.top)+margins+"px";
			//li[i].style.left="10px";
			li.eq(i).css("top",li.eq(i-2).height()+parseInt(li[i-2].style.top)+margins);
			li.eq(i).css("left","10px");	
		}
	}
	var aa=li[li.length-1].offsetHeight+parseInt(li[li.length-1].style.top)+margins;
	if(li.length>1){		
		var bb=li[li.length-2].offsetHeight+parseInt(li[li.length-2].style.top)+margins;
		if(aa>bb){
		 $("#protuct_list").height(aa+60);
		}else{
		 $("#protuct_list").height(bb+60);
		}
	}else{
		$("#protuct_list").height(aa+60)
	}
}
function new_swiper(){	
	var length=$("#mywishSwipe > .swipe-wrap > div").length;				
	var swiper="";
	for(var i=1;i<=length;i++){
		swiper=swiper+"<span></span>";
		$("#mywishSwipe > .swipe-wrap > div").eq(i-1).width($("#afui").width());
	}
	$("#mywishSwipe .swiper-pagination").html(swiper);	
	var bullets = $("#mywishSwipe .swiper-pagination span");		
	bullets[0].className = 'active';		
	var elems = document.getElementById('mywishSwipe');
	window.mywishSwipe = Swipe(elems, {
	//auto: 3000,	
	disableScroll: true,
	callback: function(index, element) {
		var i = bullets.length;
		while (i--) {
		bullets[i].className = ' ';
		}
		bullets[index].className = 'active';
	},
	transitionEnd: function(index, element) {}
	});	
	var img_swipt=$("#mywishSwipe > .swipe-wrap > div .pro_swipe").length;	
	$(".pro_swipe .swipe-wrap > div").height(($("#afui").height()-50)*.3);
	for(var j=0;j<img_swipt;j++){		
		var elemw = document.getElementById('wishSwipe_'+(j+1));
		switch(j){
		case 0:window.wishSwipe_1 = Swipe(elemw, {disableScroll: true});
		case 1:window.wishSwipe_2 = Swipe(elemw, {disableScroll: true});
		case 2:window.wishSwipe_3 = Swipe(elemw, {disableScroll: true});
		}
	}	
}

function new_swiper_1(){
	var img_swipt=$("#mywishSwipe > .swipe-wrap > div .pro_swipe").length;	
	$(".pro_swipe .swipe-wrap > div").height(($("#afui").height()-50)*.3);
	for(var j=0;j<img_swipt;j++){		
		var elemw = document.getElementById('wishSwipe_'+(j+1));
		switch(j){
		case 0:window.wishSwipe_1 = Swipe(elemw, {disableScroll: true});
		case 1:window.wishSwipe_2 = Swipe(elemw, {disableScroll: true});
		case 2:window.wishSwipe_3 = Swipe(elemw, {disableScroll: true});
		}
	}
}

function queryMenu(programCode){
	$.ajax({ 
        type: "get", 
        async: false, 
        url: baseUrl + "/json/wishwall/menu_queryProgramBaseMenu", 
        dataType: "json", 
        data: "programCode="+programCode,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert('error!'+errorThrown); 
        },
        success: function (data) {
        	var menus = data;
        	var htmlStr = "";
        	for(var i=0;i<menus.length;i++){
        		htmlStr = htmlStr + "<span class='icon colose'>"+menus[i].name+"</span>";
        		htmlStr = htmlStr + "<li>";
        		htmlStr = htmlStr + queryMenuByParentId(programCode,menus[i].id,i);
        		htmlStr = htmlStr + "</li>";
        	}
        	$(".list").html(htmlStr);
        } 
	});
	initData();
}

function queryMenuByParentId(programCode,parentId,parentIndex){
	var htmlStr = "";
	$.ajax({ 
        type: "get",
        async: false, 
        url: baseUrl + "/json/wishwall/menu_queryMenuByParentId", 
        dataType: "json", 
        data: "programCode="+programCode+"&parentId="+parentId,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert('error!'+errorThrown); 
        },
        success: function (data) {
        	var menus = data;
        	for(var i=0;i<menus.length;i++){
        		if(parentIndex == 0 && i == 0){//初始化第一次的產品列表頁
        			queryProductionByMenuId(programCode,menus[i].id);
        		}
        		htmlStr = htmlStr +"<a href='#main' onclick='queryProductionByMenuIdWithInit(\""+programCode+"\",\""+menus[i].id+"\")'>"+menus[i].name+"</a>";
        	}
        } 
	});
	return htmlStr;
}

function queryProductionByMenuId(programCode,menuId){
	var htmlStr = "";
	$.ajax({ 
        type: "get",
        async: false, 
        url: baseUrl + "/json/wishwall/production_queryProductionByMenuId", 
        dataType: "json", 
        data: "programCode="+programCode+"&menuId="+menuId,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert('error!'+errorThrown); 
        },
        success: function (data) {
        	var productions = data;
        	for(var i=0;i<productions.length;i++){
        		htmlStr = htmlStr + "<div id='"+productions[i].id+"' class='wif_div'>";
        		htmlStr = htmlStr + "<section>";
        		htmlStr = htmlStr + "<a href='javascript:void(0)' data-persist-ajax='true' data-refresh-ajax='true' onclick='initProductionId(\""+productions[i].id+"\")'>";
        		htmlStr = htmlStr + "<img src='"+productions[i].img1Url+"'>";
        		htmlStr = htmlStr + "<h1>"+productions[i].name+"</h1>";
        		htmlStr = htmlStr + "<span>￥"+productions[i].price+"</span>";
        		htmlStr = htmlStr + "</a>";
        		htmlStr = htmlStr + "<div></div>";
        		htmlStr = htmlStr + "</section>";
        		htmlStr = htmlStr + "</div>";
        	}
        } 
	});
	$("#protuct_list").html(htmlStr);
}
function queryProductionByMenuIdWithInit(programCode,menuId){
	queryProductionByMenuId(programCode,menuId);
	initData();
}
function saveWishOrder(){
	$.ui.showMask("加载中");
	var productionIdsParam = "";
	for(var i=0;i<$(".footers_tree li").length;i++){
		var imgClass = $(".footers_tree li").eq(i).attr("class");
		if(null != imgClass && "" != imgClass){
			if("" == productionIdsParam){
				productionIdsParam = imgClass.split("_")[1];
			}else{
				productionIdsParam = productionIdsParam + ","+imgClass.split("_")[1];
			}
		}
	}
	if("" != wechatIdStr){//根据openid判断订单是否存在
		$.ajax({ 
	        type: "POST",
	        async: true, 
	        url: baseUrl + "/json/wishwall/wishOrder_checkIsGuanzhu", 
	        dataType: "json", 
	        data : "code=&openId="+wechatIdStr,
	        error: function (XMLHttpRequest, textStatus, errorThrown) { 
	            alert("error!"+errorThrown); 
	        },
	        success: function (data) {
	        	if(data.state == '1'){
	        		wechatIdStr = data.openId;
	        		subscribeStr = data.subscribe;
	        		wecharNameVal = data.nickName;
	        		wecharNameValForOther = data.nickName;
	        	}else if(data.state == '0'){
	        		$("#id_alertMsg").html("faile!");
	        		$("#task_alertMsg").show();
	        	}
	        	if(subscribeStr == '1'){
	    			var wishOrderId = "";
	    			if(checkXuYuanButton()){
	    				wishOrderId = saveWishOrder2(productionIdsParam);
	    				queryWishOrderByIdForXuYuan(wishOrderId);
	    				$.ui.loadContent("#tree",false,false,"slide");
	    			}else{
	    				$.ui.loadContent("#main",false,false,"slide");
	    			}
	    			$.ui.hideMask();
	    		}else{
	    			window.location.href = guanZhuUrl;
	    		}
	        }
		});
	}else{
		window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc053aefd1dfd9fc1&redirect_uri="+baseUrl+"/html/pass.html?productionIds="+productionIdsParam+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
	}
}

function saveWishOrder2(productionIdsParam){
	var paramStr = JSON.stringify({wechatId: wechatIdStr,programCode:programCodeVal,productionIds:productionIdsParam});  
	var orderId = 0;
	$.ajax({ 
        type: "POST",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_saveWishOrder", 
        dataType: "json",
        contentType: "application/json; charset=utf-8",  
        data : paramStr,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('error!'+errorThrown); 
        },
        success: function (data) {
        	if("saveError" == data.msg){
        		alert("系统出错，请稍后重试。");
        	}else{
        		//返回订单id
            	orderId = data.state;	
        	}
        } 
	});
	return orderId;
}

function updateWishOrder(){
	$.ui.showMask("加载中");
	$.ajax({ 
        type: "POST",
        async: true, 
        url: baseUrl + "/json/wishwall/wishOrder_updateWishOrder", 
        dataType: "json",
        data : "wishDeclaration="+$("#id_xuyan").val()+"&wishOrderId="+$("#id_wishorderId").val(),
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert('error!'+errorThrown); 
        },
        success: function (data) {
        	if("saveError" == data.msg){
        		$("#id_alertMsg").html("系统出错，请稍后重试。");
        		$("#task_alertMsg").show();
        	}else if("saveSuccess" == data.msg){
        		//$("#xuyan,.xuruan").hide();
        	}
        	$("#xuyan").hide();
        	if("" != wechatIdStr){//根据openid判断订单是否存在
        		queryWishOrderById($("#id_wishorderId").val());
        		$.ui.loadContent("#trees",false,false,"slide");
        		$.ui.hideMask();
        	}else{
        		var url = escape(baseUrl+"/html/index.html?wishOrderId="+$("#id_wishorderId").val()+"&type=fenxiang");
        		window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc053aefd1dfd9fc1&redirect_uri="+url+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        	}
        } 
	});
}
function saveDiscuss(){
	var paramStr = JSON.stringify({wishOrderId: $("#id_wishorderId").val(),wechatId:wechatIdStr,comment:$("#id_zhufu_value").val(),wechatName:wecharNameVal});  
	$.ajax({ 
        type: "POST",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_saveDiscuss", 
        dataType: "json",
        contentType: "application/json; charset=utf-8",  
        data : paramStr,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert('error!'+errorThrown); 
        },
        success: function (data) {
        	if("saveError" == data.msg){
        		$("#id_alertMsg").html("系统出错，请稍后重试。");
        		$("#task_alertMsg").show();
        	}else if("saveSuccess" == data.msg){
        		$("#id_zhufu").prepend("<li><i>"+wecharNameVal+":</i>"+$("#id_zhufu_value").val()+"</li>");
        	}
        } 
	});
	$("#zhufu").hide();
}

function initProductionId(productionsId){
	$("#id_productionId").val(productionsId);
}

function checkXuYuanButton(){
	var flag = false;
	$.ajax({ 
        type: "get",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_checkXuYuanButton", 
        dataType: "json", 
        data: "wechatId=" + wechatIdStr + "&programCode=" + programCodeVal,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert('error!'+errorThrown); 
        },
        success: function (data) {
        	if("hadExists" == data.msg){
        		$("#id_alertMsg").html("请不要重复许愿，谢谢");
        		$("#task_alertMsg").show();
        	}else if("saveError" == data.msg){
        		$("#id_alertMsg").html("系统出错，请稍后重试。");
        		$("#task_alertMsg").show();
        	}else if("saveSuccess" == data.msg){
        		flag = true;
        	}
        } 
	});
	return flag;
}

function queryProductionDetailFromOrder(obj){
	$.ui.showMask("加载中");
	var nameStr = "您的好友位于第";
	if(wecharNameVal == wecharNameValForOther){//自己的需要单，不显示您的好友
		nameStr = "您位于第";
	}
	$("#id_production_list").html("");
	var currentProductionId = $(obj).find("img").attr("id").split("_")[1];
	$("#trees .tree_pro a").each(function(j){
		if(undefined != $(this).find("img").eq(0).attr("id")){
			var productionId = $(this).find("img").eq(0).attr("id").split("_")[1];
			$.ajax({ 
		        type: "get",
		        async: true, 
		        url: baseUrl + "/json/wishwall/production_queryProductionById", 
		        dataType: "json", 
		        data: "productionId="+ productionId+"&wishOrderId="+$("#id_wishorderId").val(),
		        error: function (XMLHttpRequest, textStatus, errorThrown) { 
		            alert('error!'+errorThrown); 
		        },
		        success: function (data) {
		        	var k = j + 1;
		        	var html = "<div id='id_pro_detail_"+productionId+"' title='"+k+"'>";
		        	html = html + "	<div class='hearders'>";
		        	html = html + "		<i class='icon pro_img_prev' onclick='wishSwipe_"+k+".prev()'></i>";
		        	html = html + "		<div id='wishSwipe_"+k+"' style='margin:0 auto' class='swipe pro_swipe'>";
		        	html = html + "			<div class='swipe-wrap messagewrap'>";
		        	html = html + "				<div><img src='"+data.production.img1Url+"'></div>";
		        	html = html + "			</div>";
		        	html = html + "			<section id='id_production_info'>";
		        	html = html + "				<h3>"+data.production.name+"</h3>";
		        	html = html + "				<span>"+"￥"+data.production.price+"</span>";
		        	html = html + "			</section>";
		        	html = html + "		</div>";
		        	html = html + "	<i class='icon pro_img_next' onclick='wishSwipe_"+k+".next()'></i>";
		        	html = html + "	<section>";
		        	html = html + "		<h2>许愿宣言:</h2>";
		        	if(null != data.wishOrder.wishDeclaration && 'null' != data.wishOrder.wishDeclaration){
		        		html = html + "		<p>"+data.wishOrder.wishDeclaration+"</p>";
		        	}else{
		        		html = html + "		<p></p>";
		        	}
		        	html = html + "	</section>";
		        	html = html + "	</div>";
		        	/*html = html + "	<a class='icon pro_detail'>产品详情</a>";*/
		        	html = html + "	<a class='icon pro_plist'>排行榜<span>"+nameStr+data.voteSortDtoList[0].sort+"名，加油呦！</span></a>";
		        	html = html + "	<div class='pro_plist_detail'>";
		        	html = html + "	<a class='icon pro_plists'>排行榜<span>"+nameStr+data.voteSortDtoList[0].sort+"名，加油呦！</span></a>";
		        	html = html + "	<div>";
		        	html = html + "	<ul>";
		        	for(var i = 0;i<data.voteSortDtoList.length;i++){
		        		html = html + "<li><span>第"+data.voteSortDtoList[i].sort+"名</span><span>"+data.voteSortDtoList[i].wechatName+"</span><span>"+data.voteSortDtoList[i].voteCount+"票</span></li>";
		        	}
		        	html = html + "	</ul></div></div>";
		        	html = html + "	<a class='button zhichi' onclick ='saveVoteDetail(\""+data.production.id+"\")'>支持TA的心愿</a>";
		        	html = html + "</div>";	
		        	if(currentProductionId == productionId){
			        	$("#id_production_list").prepend(html);
		        	}else{
			        	$("#id_production_list").append(html);
		        	} 
		        	new_swiper();
		        	showSortScroll();
		        	$.ui.hideMask();
		        	$.ui.loadContent("#pro_detail",false,false,"slide");
		        } 
			});
		}
	});
}

function queryWishOrderById(wishOrderId){
	wishOrderIdVal = wishOrderId;
	$("#id_wishorderId").val(wishOrderId);
	$.ajax({ 
        type: "get",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_queryWishOrderById", 
        dataType: "json", 
        data: "wishOrderId="+ wishOrderId,
        error: function (XMLHttpRequest, textStatus, errorThrown){
            alert("error!"+errorThrown);
        },
        success: function (data) {
        	for(var i = 0; i<data.productionDtoList.length;i++){
        		$("#trees .tree_pro a").eq(i).html("<img id='productionId_"+data.productionDtoList[i].production.id+"' src=\"images\/pao.png\"><img src='"+data.productionDtoList[i].production.img1Url+"'/>");
        	}
        	var zhufuHtml = "";
        	for(var j = 0; j<data.discussList.length;j++){
        		zhufuHtml = zhufuHtml + "<li><i>"+data.discussList[j].wechatName+":</i>"+data.discussList[j].comment+"</li>";
        	}
        	$("#id_zhufu").html(zhufuHtml);
        	if(wechatIdStr == data.wishOrder.wechatId){
        		$("#fengxiang").show().siblings().hide();
        		$(".showdiv").show();
        	}
        	document.title = data.wishOrder.nickName + "的心愿墙";
        	wecharNameValForOther = data.wishOrder.nickName;
        } 
	});
}

function saveVoteDetail(productionIdValue){
	if($("#id_pro_detail_"+productionIdValue).find(".zhichi_c").length > 0){//已经支持过，不需要重新查询后台数据
		$("#id_alertMsg").html("您已投票，请不要重复投票。");
		$("#task_alertMsg").show();
		return;
	}
	$.ui.showMask("加载中");
	$.ajax({ 
        type: "POST",
        async: true, 
        url: baseUrl + "/json/wishwall/wishOrder_checkIsGuanzhu", 
        dataType: "json", 
        data : "code=&openId="+wechatIdStr,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert("error!"+errorThrown); 
        },
        success: function (data) {
        	if(data.state == '1'){
        		wechatIdStr = data.openId;
        		subscribeStr = data.subscribe;
        		wecharNameVal = data.nickName;
        		wecharNameValForOther = data.nickName;
        	}else if(data.state == '0'){
        		$("#id_alertMsg").html("faile!");
        		$("#task_alertMsg").show();
        	}
        	if(subscribeStr == '1'){
        		var flag = false;
        		var paramStr = JSON.stringify({wishOrderId: $("#id_wishorderId").val(),wechatId:wechatIdStr,productionId:productionIdValue});  
        		$.ajax({ 
        	        type: "POST",
        	        async: true, 
        	        url: baseUrl + "/json/wishwall/wishOrder_saveVoteDetail", 
        	        dataType: "json",
        	        contentType: "application/json; charset=utf-8",  
        	        data : paramStr,
        	        error: function (XMLHttpRequest, textStatus, errorThrown) { 
        	            alert('error!'+errorThrown); 
        	        },
        	        success: function (data) {
        	        	if("saveSuccess" == data.msg){
        	        		$("#id_alertMsg").html("投票成功，谢谢参与。");
        	        		$("#task_alertMsg").show();
        	        		flag = true;
        	        	}else if("hadVoted" == data.msg){
        	        		$("#id_alertMsg").html("您已投票，请不要重复投票。");
        	        		$("#task_alertMsg").show();
        	        		$("#id_pro_detail_"+productionIdValue).find(".zhichi").addClass("zhichi_c").removeClass("zhichi");
        	        		$("#id_pro_detail_"+productionIdValue).find(".zhichi").text("谢谢:)");
        	        	}else if("saveError" == data.msg){
        	        		$("#id_alertMsg").html("系统出错，请稍后重试。");
        	        		$("#task_alertMsg").show();
        	        		$("#id_pro_detail_"+productionIdValue).find(".zhichi").addClass("zhichi_c").removeClass("zhichi");
        	        		$("#id_pro_detail_"+productionIdValue).find(".zhichi").text("谢谢:)");
        	        	}
        	        	$("#zhufu").hide();
                		if(!flag){
                			$.ui.hideMask();
                			return;
                		}
                		
                		var nameStr = "您的好友位于第";
                		if(wecharNameVal == wecharNameValForOther){//自己的需要单，不显示您的好友
                			nameStr = "您位于第";
                		}
                		$.ajax({ 
                	        type: "get",
                	        async: true, 
                	        url: baseUrl + "/json/wishwall/production_queryProductionById", 
                	        dataType: "json", 
                	        data: "productionId="+ productionIdValue+"&wishOrderId="+$("#id_wishorderId").val(),
                	        error: function (XMLHttpRequest, textStatus, errorThrown) { 
                	            alert('error!'+errorThrown); 
                	        },
                	        success: function (data) {
                	        	var k = $("#id_pro_detail_"+productionIdValue).attr("title");
                	        	var html = "<div class='hearders'>";
                	        	html = html + "		<i class='icon pro_img_prev' onclick='wishSwipe_"+k+".prev()'></i>";
                	        	html = html + "		<div id='wishSwipe_"+k+"' style='margin:0 auto' class='swipe pro_swipe'>";
                	        	html = html + "			<div class='swipe-wrap messagewrap'>";
                	        	html = html + "				<div><img src='"+data.production.img1Url+"'></div>";
                	        	html = html + "			</div>";
                	        	html = html + "			<section id='id_production_info'>";
                	        	html = html + "				<h3>"+data.production.name+"</h3>";
                	        	html = html + "				<span>"+"￥"+data.production.price+"</span>";
                	        	html = html + "			</section>";
                	        	html = html + "		</div>";
                	        	html = html + "	<i class='icon pro_img_next' onclick='wishSwipe_"+k+".next()'></i>";
                	        	html = html + "	<section>";
                	        	html = html + "		<h2>许愿宣言:</h2>";
                	        	if(null != data.wishOrder.wishDeclaration && 'null' != data.wishOrder.wishDeclaration){
                	        		html = html + "		<p>"+data.wishOrder.wishDeclaration+"</p>";
                	        	}else{
                	        		html = html + "		<p></p>";
                	        	}
                	        	html = html + "	</section>";
                	        	html = html + "	</div>";
                	        	/*html = html + "	<a class='icon pro_detail'>产品详情</a>";*/
                	        	html = html + "	<a class='icon pro_plist'>排行榜<span>"+nameStr+data.voteSortDtoList[0].sort+"名，加油呦！</span></a>";
                	        	html = html + "	<div class='pro_plist_detail'>";
                	        	html = html + "	<a class='icon pro_plists'>排行榜<span>"+nameStr+data.voteSortDtoList[0].sort+"名，加油呦！</span></a>";
                	        	html = html + "	<div>";
                	        	html = html + "	<ul>";
                	        	for(var i = 0;i<data.voteSortDtoList.length;i++){
                	        		html = html + "<li><span>第"+data.voteSortDtoList[i].sort+"名</span><span>"+data.voteSortDtoList[i].wechatName+"</span><span>"+data.voteSortDtoList[i].voteCount+"票</span></li>";
                	        	}
                	        	html = html + "	</ul></div></div>";
                	        	html = html + "	<a class='button zhichi_c' onclick ='saveVoteDetail(\""+data.production.id+"\")'>谢谢:)</a>";
                	        	$("#id_pro_detail_"+productionIdValue).html(html);
                	        	//图片排序
                	        	new_swiper_1();
                	        	//图片排序
            		        	showSortScroll();
                	        	$.ui.hideMask();
                	        } 
                		});
        	        } 
        		});
        	}else{
        		window.location.href = guanZhuUrl;
        	}
        }
	});
}

function queryWechatInfo(code){
	$.ajax({ 
        type: "POST",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_checkIsGuanzhu", 
        dataType: "json", 
        data : "code="+code+"&openId="+wechatIdStr,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert("error!"+errorThrown); 
        },
        success: function (data) {
        	if(data.state == '1'){
        		wechatIdStr = data.openId;
        		subscribeStr = data.subscribe;
        		wecharNameVal = data.nickName;
        		wecharNameValForOther = data.nickName;
        	}else if(data.state == '0'){
        		$("#id_alertMsg").html("faile!");
        		$("#task_alertMsg").show();
        	}
        }
	});
}

function queryWishOrderByIdForXuYuan(wishOrderId){
	wishOrderIdVal = wishOrderId;
	$("#id_wishorderId").val(wishOrderId);
	$.ajax({ 
        type: "get",
        async: false, 
        url: baseUrl + "/json/wishwall/wishOrder_queryWishOrderById", 
        dataType: "json", 
        data: "wishOrderId="+ wishOrderId,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert("error!!"+errorThrown); 
        },
        success: function (data) {
        	for(var i = 0; i<data.productionDtoList.length;i++){
        		$("#tree .tree_pro a").eq(i).html("<img id='productionId_"+data.productionDtoList[i].production.id+"' src=\"images\/pao.png\"><img src='"+data.productionDtoList[i].production.img1Url+"'/>");
        	}
    		$(".xuruan").show();
    		$(".button.fenxian").hide();
    		document.title = data.wishOrder.nickName + "的心愿墙";
        	wecharNameValForOther = data.wishOrder.nickName;
        } 
	});
}

function superAllProduction(){
	$.ui.showMask("加载中");
//	queryWechatInfo("");
	$.ajax({ 
        type: "POST",
        async: true, 
        url: baseUrl + "/json/wishwall/wishOrder_checkIsGuanzhu", 
        dataType: "json", 
        data : "code=&openId="+wechatIdStr,
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert("error!"+errorThrown); 
        },
        success: function (data) {
        	if(data.state == '1'){
        		wechatIdStr = data.openId;
        		subscribeStr = data.subscribe;
        		wecharNameVal = data.nickName;
        		wecharNameValForOther = data.nickName;
        	}else if(data.state == '0'){
        		$("#id_alertMsg").html("faile!");
        		$("#task_alertMsg").show();
        	}
        	if(subscribeStr == '1'){
        		$.ajax({ 
        	        type: "get",
        	        async: true, 
        	        url: baseUrl + "/json/wishwall/wishOrder_superAllProduction", 
        	        dataType: "json", 
        	        data: "wishOrderId=" + $("#id_wishorderId").val() + "&openId="+wechatIdStr,
        	        error: function (XMLHttpRequest, textStatus, errorThrown) { 
        	            alert('error!'+errorThrown); 
        	        },
        	        success: function (data) {
        	        	if(data.state == '1'){
        	        		$("#id_alertMsg").html("投票成功，谢谢参与。");
        	        		$("#task_alertMsg").show();
        	        	}else{
        	        		$("#id_alertMsg").html("系统出错，请稍后重试。");
        	        		$("#task_alertMsg").show();
        	        	}
        	        	$.ui.hideMask();
        	        } 
        		});
        	}else{
        		window.location.href = guanZhuUrl;
        	}
        }
	});	
}

function goToWishOrder(){
	$.ui.showMask("加载中");
	if("" != wechatIdStr){//根据openid判断订单是否存在
		$.ajax({ 
	        type: "get",
	        async: true, 
	        url: baseUrl + "/json/wishwall/wishOrder_queryWishOrderByWechatId", 
	        dataType: "json", 
	        data: "wechatId=" + wechatIdStr + "&programCode="+programCodeVal,
	        error: function (XMLHttpRequest, textStatus, errorThrown) { 
	            alert('error!'+errorThrown); 
	        },
	        success: function (data) {
	        	if(null != data.id && undefined != data.id && "" != data.id){
	        		queryWishOrderById(data.id);
	        		$.ui.loadContent("#trees",false,false,"slide");
	        	}else{
	        		$("#id_alertMsg").html("您还没有许愿单，请立即许愿。");
	        		$("#task_alertMsg").show();
	        		$.ui.loadContent("#main",false,false,"slide");
	        	}
	        	$.ui.hideMask();
	        } 
		});
	}else{
		window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc053aefd1dfd9fc1&redirect_uri="+baseUrl+"/html/queryWishOrder.html&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
	}
}

/***产品详情排名滚动***/
function showSortScroll(){
	$("a.pro_plists").click(function(){
		$(this).parents(".pro_plist_detail").hide();
	})
	$("a.pro_plist").click(function(){
    	$(this).parent().find(".pro_plist_detail").show();
    });
	var ua = navigator.userAgent.toLowerCase();
	if (/iphone|ipod/.test(ua)) {
		
	}else{
		if(/micromessenger/.test(ua)){
			var myScroller;
			$.ui.ready(function () {
			for(var i=0;i<$(".pro_plist_detail").length;i++){
				myScroller = $(".pro_plist_detail:eq("+i+") div ul").scroller();                  
				$(".tree_bottom:eq("+i+") div.scrolls ul").css("overflow", "auto");
			}
			});									
		}
	}  
}

function initFengXiang(){
	if("" != wecharNameValForOther && "" != wishOrderIdVal){
		msg_title = wecharNameValForOther + "的心愿需要你的支持";
	    msg_desc = "跪求支持，只要排队前十就可以免费领取了";
	    msg_cdn_url = baseUrl + "/html/images/fenXiangLogo.jpg";
	    var url = escape(baseUrl+"/html/index.html?wishOrderId="+wishOrderIdVal+"&type=fenxiang");
	    msg_link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc053aefd1dfd9fc1&redirect_uri="+url+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
	}else{
		msg_title = "快速许愿，丰厚的礼品等你你来拿。";
        msg_desc = "将有机会获取梦寐以求的ipone6，期待你的参与！";
        msg_cdn_url = baseUrl + "/html/images/fenXiangLogo.jpg";
        var url = escape(baseUrl+"/html/index.html?type=main");
        msg_link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc053aefd1dfd9fc1&redirect_uri="+url+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
	}
}
function xiangqing(){
	$("#taske").show();
}
